#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Profile Display
# Generated: Mon Apr 11 23:44:30 2022
##################################################

from distutils.version import StrictVersion

if __name__ == '__main__':
    import ctypes
    import sys
    if sys.platform.startswith('linux'):
        try:
            x11 = ctypes.cdll.LoadLibrary('libX11.so')
            x11.XInitThreads()
        except:
            print "Warning: failed to XInitThreads()"

from PyQt5 import Qt
from PyQt5 import Qt, QtCore
from gnuradio import analog
from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import gr
from gnuradio import qtgui
from gnuradio.eng_option import eng_option
from gnuradio.filter import firdes
from optparse import OptionParser
import numpy
import psr_display_helper  # embedded python module
import sip
import sys
import threading
import time
from gnuradio import qtgui


class profile_display(gr.top_block, Qt.QWidget):

    def __init__(self, port=1420):
        gr.top_block.__init__(self, "Profile Display")
        Qt.QWidget.__init__(self)
        self.setWindowTitle("Profile Display")
        qtgui.util.check_set_qss()
        try:
            self.setWindowIcon(Qt.QIcon.fromTheme('gnuradio-grc'))
        except:
            pass
        self.top_scroll_layout = Qt.QVBoxLayout()
        self.setLayout(self.top_scroll_layout)
        self.top_scroll = Qt.QScrollArea()
        self.top_scroll.setFrameStyle(Qt.QFrame.NoFrame)
        self.top_scroll_layout.addWidget(self.top_scroll)
        self.top_scroll.setWidgetResizable(True)
        self.top_widget = Qt.QWidget()
        self.top_scroll.setWidget(self.top_widget)
        self.top_layout = Qt.QVBoxLayout(self.top_widget)
        self.top_grid_layout = Qt.QGridLayout()
        self.top_layout.addLayout(self.top_grid_layout)

        self.settings = Qt.QSettings("GNU Radio", "profile_display")

        if StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
            self.restoreGeometry(self.settings.value("geometry").toByteArray())
        else:
            self.restoreGeometry(self.settings.value("geometry", type=QtCore.QByteArray))

        ##################################################
        # Parameters
        ##################################################
        self.port = port

        ##################################################
        # Variables
        ##################################################
        self.dlen = dlen = 1024
        self.samp_rate = samp_rate = 1e6
        self.pname = pname = psr_display_helper.get_pname(port)
        self.pacer = pacer = [1.0]*dlen

        ##################################################
        # Blocks
        ##################################################
        self.a_probe = blocks.probe_signal_vf(dlen)

        def _pacer_probe():
            while True:
                val = self.a_probe.level()
                try:
                    self.set_pacer(val)
                except AttributeError:
                    pass
                time.sleep(1.0 / (3))
        _pacer_thread = threading.Thread(target=_pacer_probe)
        _pacer_thread.daemon = True
        _pacer_thread.start()

        self.qtgui_vector_sink_f_0 = qtgui.vector_sink_f(
            dlen,
            0,
            1.0/dlen,
            "Pulsar Phase",
            "Amplitide",
            "Normalized Pulsar Profile: %s" % pname,
            1 # Number of inputs
        )
        self.qtgui_vector_sink_f_0.set_update_time(0.1)
        self.qtgui_vector_sink_f_0.set_y_axis(0, 2.0)
        self.qtgui_vector_sink_f_0.enable_autoscale(False)
        self.qtgui_vector_sink_f_0.enable_grid(True)
        self.qtgui_vector_sink_f_0.set_x_axis_units("ph")
        self.qtgui_vector_sink_f_0.set_y_axis_units("")
        self.qtgui_vector_sink_f_0.set_ref_level(0)

        labels = ['', '', '', '', '',
                  '', '', '', '', '']
        widths = [1, 1, 1, 1, 1,
                  1, 1, 1, 1, 1]
        colors = ["green", "red", "green", "black", "cyan",
                  "magenta", "yellow", "dark red", "dark green", "dark blue"]
        alphas = [1.0, 1.0, 1.0, 1.0, 1.0,
                  1.0, 1.0, 1.0, 1.0, 1.0]
        for i in xrange(1):
            if len(labels[i]) == 0:
                self.qtgui_vector_sink_f_0.set_line_label(i, "Data {0}".format(i))
            else:
                self.qtgui_vector_sink_f_0.set_line_label(i, labels[i])
            self.qtgui_vector_sink_f_0.set_line_width(i, widths[i])
            self.qtgui_vector_sink_f_0.set_line_color(i, colors[i])
            self.qtgui_vector_sink_f_0.set_line_alpha(i, alphas[i])

        self._qtgui_vector_sink_f_0_win = sip.wrapinstance(self.qtgui_vector_sink_f_0.pyqwidget(), Qt.QWidget)
        self.top_layout.addWidget(self._qtgui_vector_sink_f_0_win)
        self.blocks_throttle_0 = blocks.throttle(gr.sizeof_float*1, samp_rate,True)
        self.blocks_stream_to_vector_0 = blocks.stream_to_vector(gr.sizeof_float*1, dlen)
        self.blocks_multiply_const_vxx_0 = blocks.multiply_const_vff((psr_display_helper.get_profile(pacer,port,dlen)))
        self.analog_const_source_x_0 = analog.sig_source_f(0, analog.GR_CONST_WAVE, 0, 0, 1.0)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.analog_const_source_x_0, 0), (self.blocks_throttle_0, 0))
        self.connect((self.blocks_multiply_const_vxx_0, 0), (self.a_probe, 0))
        self.connect((self.blocks_multiply_const_vxx_0, 0), (self.qtgui_vector_sink_f_0, 0))
        self.connect((self.blocks_stream_to_vector_0, 0), (self.blocks_multiply_const_vxx_0, 0))
        self.connect((self.blocks_throttle_0, 0), (self.blocks_stream_to_vector_0, 0))

    def closeEvent(self, event):
        self.settings = Qt.QSettings("GNU Radio", "profile_display")
        self.settings.setValue("geometry", self.saveGeometry())
        event.accept()

    def get_port(self):
        return self.port

    def set_port(self, port):
        self.port = port
        self.set_pname(psr_display_helper.get_pname(self.port))
        self.blocks_multiply_const_vxx_0.set_k((psr_display_helper.get_profile(self.pacer,self.port,self.dlen)))

    def get_dlen(self):
        return self.dlen

    def set_dlen(self, dlen):
        self.dlen = dlen
        self.set_pacer([1.0]*self.dlen)
        self.qtgui_vector_sink_f_0.set_x_axis(0, 1.0/self.dlen)
        self.blocks_multiply_const_vxx_0.set_k((psr_display_helper.get_profile(self.pacer,self.port,self.dlen)))

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.blocks_throttle_0.set_sample_rate(self.samp_rate)

    def get_pname(self):
        return self.pname

    def set_pname(self, pname):
        self.pname = pname

    def get_pacer(self):
        return self.pacer

    def set_pacer(self, pacer):
        self.pacer = pacer
        self.blocks_multiply_const_vxx_0.set_k((psr_display_helper.get_profile(self.pacer,self.port,self.dlen)))


def argument_parser():
    parser = OptionParser(usage="%prog: [options]", option_class=eng_option)
    parser.add_option(
        "", "--port", dest="port", type="intx", default=1420,
        help="Set XMLRPC port [default=%default]")
    return parser


def main(top_block_cls=profile_display, options=None):
    if options is None:
        options, _ = argument_parser().parse_args()

    if StrictVersion("4.5.0") <= StrictVersion(Qt.qVersion()) < StrictVersion("5.0.0"):
        style = gr.prefs().get_string('qtgui', 'style', 'raster')
        Qt.QApplication.setGraphicsSystem(style)
    qapp = Qt.QApplication(sys.argv)

    tb = top_block_cls(port=options.port)
    tb.start()
    tb.show()

    def quitting():
        tb.stop()
        tb.wait()
    qapp.aboutToQuit.connect(quitting)
    qapp.exec_()


if __name__ == '__main__':
    main()
